﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using emp.Utility;
using System.Collections;
using emp.Model.SysManage;
using System.Data;
using System.Data.SqlClient;
using emp.SqlServerDAL;
using System.Configuration;

public partial class crm_salienceMember_SalienceMemberList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {   
        if (!IsPostBack)
        {
            
        }
    }


    protected void gvquestlist_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        GridView theGrid = sender as GridView;
        int newPageIndex = 0;
        if (-2 == e.NewPageIndex)
        {
            TextBox txtNewPageIndex = null;
            GridViewRow pagerRow = theGrid.BottomPagerRow;
            if (null != pagerRow)
            {
                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
            }
            if (null != txtNewPageIndex)
            {
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
            }
        }
        else
        {
            newPageIndex = e.NewPageIndex;
        }

        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
        theGrid.PageIndex = newPageIndex;
        inital();
    }

    void inital()
    {
        Session["int"] = getDt().Rows.Count;
        this.gvquestlist.DataSource = getDt();
        this.gvquestlist.DataBind();
        if (getDt().Rows.Count == 0)
        {
            LBjl.Visible = true;
        }
        else
        {
            LBjl.Visible = false;
        }
    
    }

   
   

    private DataTable getDt() 
    {
        DataTable dt = null;
        string sql = " select * from [INDIVIDUALS]  WHERE 1=1";

        if (IND_URN.Text!="") 
        {
            sql += " and IND_CUSTOMER_CODE='" + IND_URN.Text + "'";
        }

        if (IND_NAME.Text != "")
        {
            sql += " and IND_NAME='" + IND_URN.Text + "'";
        }
      
        if (IND_TEL_AREA_1.Text != "")
        {
            sql += " and IND_TEL_AREA='" + IND_URN.Text + "'";
        }
        if (IND_TEL_1.Text != "")
        {
            sql += " and IND_TEL='" + IND_URN.Text + "'";
        }
        if (IND_CELLPHONE_1.Text != "")
        {
            sql += " and IND_CELLPHONE='" + IND_URN.Text + "'";
        }
        else
        {
            if (IND_CELLPHONE_1_T.Checked)
            {
                sql += " and IND_CELLPHONE<>'' and IND_CELLPHONE is not null";
            }
        }
        if (IND_EMAIL_1.Text != "")
        {
            sql += " and IND_EMAIL='" + IND_URN.Text + "'";
        }
        else
        {
            if (IND_EMAIL_1_T.Checked)
            {
                sql += " and IND_EMAIL<>'' and IND_EMAIL is not null";
            }
        }


        if (IND_GENDER1.Checked)
        {
            sql += " and IND_GENDER='M'";
        }

        if (IND_GENDER2.Checked)
        {
            sql += " and IND_GENDER='F'";
        }


        //if (region.SelectedValue!="")
        //{
        //    sql += " and IND_REGION='" + region.SelectedValue + "'";
        //}


        if (IND_PROVINCE.SelectedValue != "")
        {
            sql += " and IND_PROVINCE='" + IND_PROVINCE.SelectedValue + "'";
        }
        if (ind_city.SelectedValue != "")
        {
            sql += " and IND_CITY='" + ind_city.SelectedValue + "'";
        }




        UserInfo myUserInfo = new UserInfo();
        if ((UserInfo)Session["USER_KEY"] != null)
        {
            myUserInfo = (UserInfo)Session["USER_KEY"];

        }
        string uid = myUserInfo.UserID.ToString();
        string uids = ConfigurationSettings.AppSettings["publicRole"];
        string[] op = uids.Split(',');
        for (int i = 0; i < op.Length; i++)
        {

            string sql11 = "SELECT * from [SYSTEM_ROLE_USER] where [role_uid]='" + op[i] + "' and [user_uid]='" + myUserInfo.UserID.ToString() + "'";
            System.Data.DataTable dt1 = getTableInfo(sql11);

            if (dt1.Rows.Count > 0)
            {
                uid = "";
            }
        }

        System.Data.DataTable dt1111 = getTableInfo("select * from [SYSTEM_USER_TBL] where [user_uid]=" + myUserInfo.UserID.ToString());
        if (dt1111.Rows.Count > 0 && dt1111.Rows[0]["user_type"].ToString() == "MANAGER")
        {
            sql += " and IND_DEALER_UID in (select [user_uid] from SYSTEM_USER_TBL where [user_region_id]='" + myUserInfo.UserID.ToString() + "' ) ";
        }
        else
        {
            if (uid != null && uid != "")
            {
                sql += " and IND_DEALER_UID = '" + uid + "' ";
            }
        }



        sql += "   order by [IND_URN] desc";

        dt = getTableInfo(sql);
        return dt;
    }


    protected void selectBtn_Click(object sender, EventArgs e)
    {

        inital();
        
    }


    private DataTable getTableInfo(string sql)
    {
        DataSet data = new DataSet();
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            try
            {
                sda.SelectCommand = new SqlCommand();
                sda.SelectCommand.CommandText = sql;
                sda.SelectCommand.Connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC);
                sda.Fill(data);
            }
            catch { }
        }
        return data.Tables[0];
    }
    protected void IND_PROVINCE_SelectedIndexChanged(object sender, EventArgs e)
    {

        string sql = "SELECT  [CITY_ID],[CITY_NAME] FROM [CITY] WHERE [CITY_PVE_CODE] in (select [PVE_ID] from [PROVINCE] where [PVE_NAME]='" + IND_PROVINCE.SelectedValue + "')";
        System.Data.DataTable dt1 = getTableInfo(sql);
        this.ind_city.Items.Clear();
        this.ind_city.Items.Add(new ListItem("请选择", ""));
        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            this.ind_city.Items.Add(new ListItem(dt1.Rows[i]["CITY_NAME"].ToString(), dt1.Rows[i]["CITY_NAME"].ToString()));

        }


    }
    protected void region_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sql = "select [PVE_ID],[PVE_NAME] from [PROVINCE]";
        System.Data.DataTable dt1 = getTableInfo(sql);
        this.IND_PROVINCE.Items.Clear();
        this.IND_PROVINCE.Items.Add(new ListItem("请选择", ""));
        // this.TCI_CAR_MODEL.Items.Add(new ListItem("请选择", "请选择"));
        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            this.IND_PROVINCE.Items.Add(new ListItem(dt1.Rows[i]["PVE_NAME"].ToString(), dt1.Rows[i]["PVE_NAME"].ToString()));
        }
    }
    protected void selectBtn0_Click(object sender, EventArgs e)
    {
        Response.Redirect("individuals_adds.aspx", true);
    }
}
